package com.woniu.controller;

import cn.hutool.jwt.JWTUtil;
import com.woniu.dto.LoginFormDto;
import com.woniu.pojo.LoginForm;
import com.woniu.pojo.OrdersUums;
import com.woniu.service.OrdersUumsService;
import com.woniu.until.RandomUtil;
import com.woniu.until.ResponseData;
import com.woniu.until.Sample;
import com.woniu.until.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/yzm")
public class YzmController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private OrdersUumsService ordersUumsService;

    @PostMapping("/getYzm")
    public ResponseData<?> getYzm(@RequestBody LoginFormDto loginForm) {
        if (StringUtil.isBlank(loginForm.getPhoneNumber())) {
            return new ResponseData<>().fail();
        }
        OrdersUums ordersUumsDb = ordersUumsService.queryByTel(loginForm.getPhoneNumber());
        if (ordersUumsDb == null) {
            return new ResponseData<>().fail();
        }
        String yzm = RandomUtil.getFourBitRandom();
        redisTemplate.opsForValue().set(loginForm.getPhoneNumber(), yzm, Duration.ofMillis(50 * 1000));
        try {
            Sample.send(loginForm.getPhoneNumber(), yzm);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new ResponseData<>().ok();
    }

    @PostMapping("/loginYzm")
    public ResponseData<?> loginYzm(@RequestBody LoginFormDto loginForm) {
        String jwt = null;
        if (StringUtil.isBlank(loginForm.getPhoneNumber())) {
            return new ResponseData<>().fail();
        }
        String yzm = (String) redisTemplate.opsForValue().get(loginForm.getPhoneNumber());
        if (yzm == null) {
            return new ResponseData<>(2001, "fail", "验证码失效");
        }
        OrdersUums ordersUums = ordersUumsService.queryByTel(loginForm.getPhoneNumber());
        if (yzm.equals(loginForm.getVerificationCode())) {

            Map map = new HashMap<>();
            map.put("userId", ordersUums.getId());
            map.put("account", ordersUums.getAccount());

            String token = JWTUtil.createToken(map, ordersUums.getUsername().getBytes());
            redisTemplate.opsForValue().set("jwt" + ordersUums.getUsername(), token);
        }
        return new ResponseData<>().ok(jwt);
    }

    @PostMapping("/login")
    public ResponseData<?> login(@RequestBody LoginForm loginForm) {
        String jwt = null;
        if (StringUtil.isBlank(loginForm.getUsername()) | StringUtil.isBlank(loginForm.getPassword())) {

            return new ResponseData<>().fail();
        }
        OrdersUums ordersUumsDb = ordersUumsService.queryByAccount(loginForm.getUsername());

        if (ordersUumsDb == null) {
            return new ResponseData<>().fail();
        }

        if (!ordersUumsDb.getPwd().equals(loginForm.getPassword())) {
            return new ResponseData<>().fail();
        }

        Map map = new HashMap<>();
        map.put("userId", ordersUumsDb.getId());
        map.put("account", ordersUumsDb.getAccount());

        String token = JWTUtil.createToken(map, ordersUumsDb.getUsername().getBytes());
        redisTemplate.opsForValue().set("jwt" + ordersUumsDb.getUsername(), token);

        return new ResponseData<>().ok(jwt);
    }
}
